package com.wbao.dianniu.im;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.FriendListAdapter;
import com.wbao.dianniu.data.HeadData;
import com.wbao.dianniu.listener.IFriendListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FriendListManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.update.FriendListHelper;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.utils.ActivityUtils;

import java.util.List;

/**
 * Created by yejianjun on 2018/7/12.
 */

public class FriendListActivity extends BaseActivity implements View.OnClickListener, IFriendListListener, FriendListAdapter.OnItemClickListener, FriendListHelper.IFriendChangeListener {
    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;
    private FriendListManager manager;

    private LinearLayout mFollow;
    private LinearLayout addFriends;
    private Button addFriendBtn;
    private TextView noDataTV;

    private FriendListAdapter adapter;
    private int ROWS = 10;
    private PullToRefreshListView pullListView;
    private ListView myListView;
    //是否是下拉刷新
    private boolean isPullDown = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_friend_list, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.my_friends));
        initWattingView();
        initView();
        initRecycler();
        initManager();
        reqPullDownData();
    }

    private void initWattingView() {
        waittingLayout = (LinearLayout) findViewById(R.id.waitting_layout);
        waittingProBar = (ProgressBar) findViewById(R.id.waitting_progress);
        waittingTV = (TextView) findViewById(R.id.waitting_text_desc);
    }

    private void initView() {
        mFollow = (LinearLayout) findViewById(R.id.follow_linearlayout);
        addFriends = (LinearLayout) findViewById(R.id.add_friend_injoin);
        //我的关注
        mFollow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(Const.INTENT_ID, GlobalContext.getAccountId());
                intent.setClass(FriendListActivity.this, FollowListActivity.class);
                startActivity(intent);
            }
        });
        //邀请好友加入
        addFriends.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ShareSDKManager shareSDKManager = ShareSDKManager.getInstance();
                shareSDKManager.showShare(FriendListActivity.this, getResources().getString(R.string.app_share_content), 3, GlobalContext.cdndownUrl + "logo.png", GlobalContext.getAccountId());
            }
        });

        addFriendBtn = getRightTextButton();
        addFriendBtn.setText(getResources().getString(R.string.add_friend_title));
        addFriendBtn.setOnClickListener(this);
        noDataTV = (TextView) findViewById(R.id.no_find_data);
    }

    private void initRecycler() {
        pullListView = (PullToRefreshListView) findViewById(R.id.friend_list_recycler);
        pullListView.setMode(PullToRefreshListView.Mode.BOTH);
        pullListView.setOnRefreshListener(onRefreshListener2);
        myListView = pullListView.getRefreshableView();
        registerForContextMenu(myListView);
        adapter = new FriendListAdapter(this);
        adapter.setItemClickListener(this);
        myListView.setAdapter(adapter);

    }

    /*PullToRefreshBase.OnRefreshListener onRefreshListener = new PullToRefreshBase.OnRefreshListener() {
        @Override
        public void onRefresh() {
            reqData();
        }
    };*/

    PullToRefreshBase.OnRefreshListener2 onRefreshListener2 = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh() {
            reqPullDownData();
        }

        @Override
        public void onPullUpToRefresh() {
            reqPullUpData();
        }
    };


    private void initManager() {
        manager = new FriendListManager(this);
        manager.addFriendListener(this);
        FriendListHelper.getInstance().addListener(this);
    }

    private void reqPullDownData() {
        isPullDown = true;
        manager.friendList(GlobalContext.getAccountId(), 0, ROWS);
    }

    private void reqPullUpData() {
        isPullDown = false;
        manager.friendList(GlobalContext.getAccountId(), adapter.getList().size(), ROWS);
    }


    @Override
    public void onFriendFailure(int errorCode, String errorMsg) {
        waittingLayout.setVisibility(View.VISIBLE);
        waittingProBar.setVisibility(View.GONE);
        waittingTV.setText(errorMsg == null ? "" : errorMsg);
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onFriendSuccess(List<HeadData> objdata) {
        waittingLayout.setVisibility(View.GONE);
        pullListView.onRefreshComplete(false, false);
        if (isPullDown) {
            adapter.setData(objdata);
        } else {
            adapter.addData(objdata);
        }
        if (adapter.getList().size() == 0) {
            noDataTV.setVisibility(View.VISIBLE);
        } else {
            noDataTV.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.right_button:
                // 加好友列表
                Intent intent = new Intent();
                intent.setClass(FriendListActivity.this, AddFriendActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeFriendListener(this);
        }
        FriendListHelper.getInstance().removeListener(this);
    }

    @Override
    public void itemClick(View v) {
        int position;
        switch (v.getId()) {
            case R.id.friend_list_layout:
                position = (Integer) v.getTag(R.id.friend_list_layout);
                ActivityUtils.intoHeadInfoActivity(adapter.getList().get(position).friendId, FriendListActivity.this);
                break;

            default:
                break;
        }
    }

    @Override
    public void deleteFriend(int friendid) {
        adapter.deleteFriend(friendid);
    }

    @Override
    public void cancelFollow(int friendId) {

    }
}
